package com.swgk.core.view.adapter;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;

import androidx.annotation.LayoutRes;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.swgk.core.R;
import com.swgk.core.databinding.LibraryItemTopViewBinding;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DisplayUtil;

import java.util.List;

/**
 * @author : younghow
 * @date : 2021/11/19 10:57
 * description:
 */
public class TopViewAdapter<D> extends BaseRecycleAdapter<List<D>> {

    private ItemClick<D> onItemClick;

    public void setOnItemClick(ItemClick<D> onItemClick){
        this.onItemClick = onItemClick;
    }

    public TopViewAdapter(Context context, List<List<D>> datas) {
        super(context, R.layout.library_item_top_view, datas);
    }

    public TopViewAdapter(Context context, List<List<D>> datas,@LayoutRes int layoutId) {
        super(context, layoutId, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, List<D> entity) {
        LibraryItemTopViewBinding mBinding = (LibraryItemTopViewBinding) holder.getmBinding();

        DisplayMetrics dm;
        dm = context.getResources().getDisplayMetrics();
        int screenWidth = dm.widthPixels - DisplayUtil.dip2px(context,40);

        if (entity.size() == 1){
            if (entity.get(0) instanceof Integer){
                mBinding.ivTeamMainPicture1.setBackgroundResource((Integer) entity.get(0));
            }

            if(entity.get(0) instanceof String){
                Glide.with(context).load((String) entity.get(0)).into(mBinding.ivTeamMainPicture1);
            }

            RecyclerView.LayoutParams para1;
            para1 = (RecyclerView.LayoutParams) mBinding.llPicture.getLayoutParams();
            para1.width = (int) ((screenWidth/6.0f) * 1);
            mBinding.llPicture.setLayoutParams(para1);
            mBinding.rl1.setVisibility(View.VISIBLE);
            mBinding.rl2.setVisibility(View.GONE);
            mBinding.rl3.setVisibility(View.GONE);
            mBinding.rl4.setVisibility(View.GONE);
            mBinding.rl5.setVisibility(View.GONE);
            mBinding.rl6.setVisibility(View.GONE);
        }

        if (entity.size() == 2){

            if (entity.get(0) instanceof Integer){
                mBinding.ivTeamMainPicture1.setBackgroundResource((Integer)entity.get(0));
                mBinding.ivTeamMainPicture2.setBackgroundResource((Integer)entity.get(1));
            }

            if(entity.get(0) instanceof String){
                Glide.with(context).load((String) entity.get(0)).into(mBinding.ivTeamMainPicture1);
                Glide.with(context).load((String) entity.get(1)).into(mBinding.ivTeamMainPicture2);
            }


            RecyclerView.LayoutParams para1;
            para1 = (RecyclerView.LayoutParams) mBinding.llPicture.getLayoutParams();
            para1.width = (int) ((screenWidth/6.0f) * 2);
            mBinding.llPicture.setLayoutParams(para1);
            mBinding.rl1.setVisibility(View.VISIBLE);
            mBinding.rl2.setVisibility(View.VISIBLE);
            mBinding.rl3.setVisibility(View.GONE);
            mBinding.rl4.setVisibility(View.GONE);
            mBinding.rl5.setVisibility(View.GONE);
            mBinding.rl6.setVisibility(View.GONE);
        }

        if (entity.size() == 3){
            if (entity.get(0) instanceof Integer){
                mBinding.ivTeamMainPicture1.setBackgroundResource((Integer)entity.get(0));
                mBinding.ivTeamMainPicture2.setBackgroundResource((Integer)entity.get(1));
                mBinding.ivTeamMainPicture3.setBackgroundResource((Integer)entity.get(2));
            }

            if(entity.get(0) instanceof String){
                Glide.with(context).load((String) entity.get(0)).into(mBinding.ivTeamMainPicture1);
                Glide.with(context).load((String) entity.get(1)).into(mBinding.ivTeamMainPicture2);
                Glide.with(context).load((String) entity.get(2)).into(mBinding.ivTeamMainPicture3);
            }

            RecyclerView.LayoutParams para1;
            para1 = (RecyclerView.LayoutParams) mBinding.llPicture.getLayoutParams();
            para1.width = (int) ((screenWidth/6.0f) * 3);
            mBinding.llPicture.setLayoutParams(para1);
            mBinding.rl1.setVisibility(View.VISIBLE);
            mBinding.rl2.setVisibility(View.VISIBLE);
            mBinding.rl3.setVisibility(View.VISIBLE);
            mBinding.rl4.setVisibility(View.GONE);
            mBinding.rl5.setVisibility(View.GONE);
            mBinding.rl6.setVisibility(View.GONE);
        }

        if (entity.size() == 4){
            if (entity.get(0) instanceof Integer){
                mBinding.ivTeamMainPicture1.setBackgroundResource((Integer)entity.get(0));
                mBinding.ivTeamMainPicture2.setBackgroundResource((Integer)entity.get(1));
                mBinding.ivTeamMainPicture3.setBackgroundResource((Integer)entity.get(2));
                mBinding.ivTeamMainPicture4.setBackgroundResource((Integer)entity.get(3));
            }

            if(entity.get(0) instanceof String){
                Glide.with(context).load((String) entity.get(0)).into(mBinding.ivTeamMainPicture1);
                Glide.with(context).load((String) entity.get(1)).into(mBinding.ivTeamMainPicture2);
                Glide.with(context).load((String) entity.get(2)).into(mBinding.ivTeamMainPicture3);
                Glide.with(context).load((String) entity.get(3)).into(mBinding.ivTeamMainPicture4);
            }

            RecyclerView.LayoutParams para1;
            para1 = (RecyclerView.LayoutParams) mBinding.llPicture.getLayoutParams();
            para1.width = (int) ((screenWidth/6.0f) * 4);
            mBinding.llPicture.setLayoutParams(para1);
            mBinding.rl1.setVisibility(View.VISIBLE);
            mBinding.rl2.setVisibility(View.VISIBLE);
            mBinding.rl3.setVisibility(View.VISIBLE);
            mBinding.rl4.setVisibility(View.VISIBLE);
            mBinding.rl5.setVisibility(View.GONE);
            mBinding.rl6.setVisibility(View.GONE);
        }

        if (entity.size() == 5){
            if (entity.get(0) instanceof Integer){
                mBinding.ivTeamMainPicture1.setBackgroundResource((Integer)entity.get(0));
                mBinding.ivTeamMainPicture2.setBackgroundResource((Integer)entity.get(1));
                mBinding.ivTeamMainPicture3.setBackgroundResource((Integer)entity.get(2));
                mBinding.ivTeamMainPicture4.setBackgroundResource((Integer)entity.get(3));
                mBinding.ivTeamMainPicture5.setBackgroundResource((Integer)entity.get(4));
            }

            if(entity.get(0) instanceof String){
                Glide.with(context).load((String) entity.get(0)).into(mBinding.ivTeamMainPicture1);
                Glide.with(context).load((String) entity.get(1)).into(mBinding.ivTeamMainPicture2);
                Glide.with(context).load((String) entity.get(2)).into(mBinding.ivTeamMainPicture3);
                Glide.with(context).load((String) entity.get(3)).into(mBinding.ivTeamMainPicture4);
                Glide.with(context).load((String) entity.get(4)).into(mBinding.ivTeamMainPicture5);
            }

            RecyclerView.LayoutParams para1;
            para1 = (RecyclerView.LayoutParams) mBinding.llPicture.getLayoutParams();
            para1.width = (int) ((screenWidth/6.0f) * 5);
            mBinding.llPicture.setLayoutParams(para1);
            mBinding.rl1.setVisibility(View.VISIBLE);
            mBinding.rl2.setVisibility(View.VISIBLE);
            mBinding.rl3.setVisibility(View.VISIBLE);
            mBinding.rl4.setVisibility(View.VISIBLE);
            mBinding.rl5.setVisibility(View.VISIBLE);
            mBinding.rl6.setVisibility(View.GONE);
        }

        if (entity.size() == 6){
            if (entity.get(0) instanceof Integer){
                mBinding.ivTeamMainPicture1.setBackgroundResource((Integer)entity.get(0));
                mBinding.ivTeamMainPicture2.setBackgroundResource((Integer)entity.get(1));
                mBinding.ivTeamMainPicture3.setBackgroundResource((Integer)entity.get(2));
                mBinding.ivTeamMainPicture4.setBackgroundResource((Integer)entity.get(3));
                mBinding.ivTeamMainPicture5.setBackgroundResource((Integer)entity.get(4));
                mBinding.ivTeamMainPicture6.setBackgroundResource((Integer)entity.get(5));
            }

            if(entity.get(0) instanceof String){
                Glide.with(context).load((String) entity.get(0)).into(mBinding.ivTeamMainPicture1);
                Glide.with(context).load((String) entity.get(1)).into(mBinding.ivTeamMainPicture2);
                Glide.with(context).load((String) entity.get(2)).into(mBinding.ivTeamMainPicture3);
                Glide.with(context).load((String) entity.get(3)).into(mBinding.ivTeamMainPicture4);
                Glide.with(context).load((String) entity.get(4)).into(mBinding.ivTeamMainPicture5);
                Glide.with(context).load((String) entity.get(5)).into(mBinding.ivTeamMainPicture6);
            }

            RecyclerView.LayoutParams para1;
            para1 = (RecyclerView.LayoutParams) mBinding.llPicture.getLayoutParams();
            para1.width = (int) ((screenWidth/6.0f) * 6);
            mBinding.llPicture.setLayoutParams(para1);
            mBinding.rl1.setVisibility(View.VISIBLE);
            mBinding.rl2.setVisibility(View.VISIBLE);
            mBinding.rl3.setVisibility(View.VISIBLE);
            mBinding.rl4.setVisibility(View.VISIBLE);
            mBinding.rl5.setVisibility(View.VISIBLE);
            mBinding.rl6.setVisibility(View.VISIBLE);
        }

        mBinding.ivTeamMainPicture1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClick.itemClick(entity.get(0),entity,holder.getIndex_position(),0);
            }
        });

        mBinding.ivTeamMainPicture2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClick.itemClick(entity.get(1),entity,holder.getIndex_position(),1);
            }
        });

        mBinding.ivTeamMainPicture3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClick.itemClick(entity.get(2),entity,holder.getIndex_position(),2);
            }
        });

        mBinding.ivTeamMainPicture4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClick.itemClick(entity.get(3),entity,holder.getIndex_position(),3);
            }
        });

        mBinding.ivTeamMainPicture5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClick.itemClick(entity.get(4),entity,holder.getIndex_position(),4);
            }
        });

        mBinding.ivTeamMainPicture6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClick.itemClick(entity.get(5),entity,holder.getIndex_position(),5);
            }
        });

    }

    public interface ItemClick<D>{
        void itemClick(D drawable,List<D> list,int parentPosition,int childPosition);
    }
}
